package com.yueshop.seckill.scheduled;


import com.yueshop.seckill.constant.SeckillConstant;
import com.yueshop.seckill.service.SeckillService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SeckillSkuScheduled {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    SeckillService seckillService;

    @Scheduled(cron = "0/5 * * * * ?")//每晚3点上架
    public void upSeckillSkuLatest3Days(){
        RLock lock = redissonClient.getLock(SeckillConstant.SECKILL_UPLOAD_LOCK);
        lock.lock(10, TimeUnit.SECONDS);
        //执行业务逻辑 上架最今三天秒杀的商品
        try {
           // System.out.println("秒杀商品正在上架.....");
            seckillService.upSeckillSkuLatest3Days();
        } finally {
            lock.unlock();//最终一定会解锁
        }
    }
}
